package com.ikas.ai.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.ikas.ai.utils.DateTimeUtil;

import java.io.Serializable;

/**
 * 实体类基类
 * @author HXH
 */
public class Entity<T extends Entity<?>> extends Model<T> implements Serializable {

    private static final long serialVersionUID = 5592466455740451266L;

    /**
     * 转成JSON对象
     * @return
     */
    public JSONObject toJSON() {
        String text = JSON.toJSONString(this);
        JSONObject json = JSONObject.parseObject(text);
        return json;
    }

    /**
     * 转成JSON字符串
     * @return
     */
    public String toJSONString() {
        JSON.DEFFAULT_DATE_FORMAT = DateTimeUtil.getDateTimePattern();
        int feature = SerializerFeature.config(JSON.DEFAULT_GENERATE_FEATURE, SerializerFeature.WriteEnumUsingName, false);
        SerializerFeature[] features = {SerializerFeature.WriteDateUseDateFormat};
        String json = JSON.toJSONString(this, feature, features);
        return json;
    }
}
